Page({
  data: {
    content: '',
    problemId: ''
  },

  handleContentInput(e) {
    this.setData({ content: e.detail.value });
  },

  onLoad(options) {
    console.log('接收到的参数:', options.id);//22
    
    // 1. 优先处理参数
    if (!options.id) {
      wx.showToast({ title: '参数错误', icon: 'none' });
      return;
    }
    this.setData({ problemId: options.id });
    console.log('hello的problemId:', this.data.problemId);  //22
    // 2. 再检查登录状态
    const app = getApp();
    if (!app.globalData.loginUserInfo) {
      wx.redirectTo({
        url: `/pages/login/login?url=/index/pages/reply/reply?id=${options.id}`,  // 使用options.id
      });
      return;
    }
  },

  send() {
    console.log('发送时的problemId:', this.data.problemId);  // 调试
    
    const { content, problemId } = this.data;
    const app = getApp();
    const userId = app.globalData.userId;

    if (!content.trim()) {
      wx.showToast({ title: '内容不能为空', icon: 'none' });
      return;
    }

    wx.request({
      url: 'http://localhost:8080/reply',
      method: 'POST',
      data: { problemId, content, userId },
      success: (res) => {
        if (res.data.code === 200) {
          wx.showToast({ title: '回复成功', icon: 'success' });
          wx.navigateTo({
            url: `/index/pages/problemDetails/problemDetails?id=${problemId}`,
          });
        } else {
          wx.showToast({ title: res.data.message || '回复失败', icon: 'none' });
        }
      },
      fail: (err) => {
        console.error('请求失败', err);
        wx.showToast({ title: '网络错误', icon: 'none' });
      }
    });
  }
});